//
//  UILabel+Additions.m
//  Yanwei
//
//  Created by 言味 on 16/9/21.
//  Copyright © 2016年 DCloud. All rights reserved.
//

#import "UILabel+Additions.h"

@implementation UILabel (Additions)

+ (UILabel*)singleLineText:(NSString*)text font:(UIFont*)font wid:(CGFloat)wid {
    return [UILabel singleLineText:text font:font wid:wid color:[UIColor blackColor]];
}
+ (UILabel*)singleLineText:(NSString*)text font:(UIFont*)font wid:(CGFloat)wid color:(UIColor*)color {
    return [UILabel linesText:text font:font wid:wid lines:1 color:color];
}

+ (UILabel*)multLinesText:(NSString*)text font:(UIFont*)font wid:(CGFloat)wid {
    return [UILabel multLinesText:text font:font wid:wid color:[UIColor blackColor]];
}
+ (UILabel*)multLinesText:(NSString*)text font:(UIFont*)font wid:(CGFloat)wid color:(UIColor*)color {
    return [UILabel linesText:text font:font wid:wid lines:0 color:color];
}

+ (UILabel*)linesText:(NSString*)text font:(UIFont*)font wid:(CGFloat)wid lines:(int)lines {
    return [UILabel linesText:text font:font wid:wid lines:lines color:[UIColor blackColor]];
}
+ (UILabel*)linesText:(NSString*)text font:(UIFont*)font wid:(CGFloat)wid lines:(int)lines color:(UIColor*)color {
    CGFloat maxH = 0;
    if (lines > 0) {
        maxH = (font.pointSize + 2) * lines + 3;
    } else {
        maxH = 6000;
    }
    CGSize size = CGSizeMake(wid, maxH);
    
    NSMutableParagraphStyle * paragraph = [NSMutableParagraphStyle new];
    paragraph.alignment = NSTextAlignmentLeft;
    if (lines == 1) {
        paragraph.lineBreakMode = NSLineBreakByTruncatingTail;
    } else {
        paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    }
    NSDictionary * attr = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraph};
    size = [text length] > 0 ? [text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin) attributes:attr context:nil].size : CGSizeZero;
    UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    lab.numberOfLines = lines;
    lab.backgroundColor = [UIColor clearColor];
    lab.lineBreakMode = NSLineBreakByTruncatingTail;
    lab.textAlignment = NSTextAlignmentLeft;
    lab.textColor = color;
    lab.font = font;
    lab.text = text;
    return lab;
}

+ (UILabel*)labWithWidth:(CGFloat)wid font:(UIFont*)font {
    return [self labWithWidth:wid font:font color:[UIColor blackColor]];
}
+ (UILabel*)labWithWidth:(CGFloat)wid font:(UIFont*)font color:(UIColor*)color {
    UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wid, font.lineHeight)];
    lab.numberOfLines = 1;
    lab.backgroundColor = [UIColor clearColor];
    lab.lineBreakMode = NSLineBreakByTruncatingTail;
    lab.textAlignment = NSTextAlignmentLeft;
    lab.textColor = color;
    lab.font = font;
    return lab;
}

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    if (label.text.hasValue) {
        NSString *labelText = label.text;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:space];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
        label.attributedText = attributedString;
    }
}

+ (void)changeLineSpaceAndCenterForLabel:(UILabel *)label WithSpace:(float)space {
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
}

+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

- (void)setAttributedText:(NSString *)text
                     font:(UIFont *)font
                 iconFont:(UIFont *)iconFont
               lineOffset:(CGFloat)lineOffset{
    // 同label不同字体大小
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = [text rangeOfString:@" "];
    [attrString addAttribute:NSFontAttributeName
                       value:font
                       range:NSMakeRange(0, range.location)];
    [attrString addAttribute:NSFontAttributeName
                       value:iconFont
                       range:NSMakeRange(range.location, text.length - range.location)];
    [attrString addAttribute:NSBaselineOffsetAttributeName
                       value:@(lineOffset)
                       range:NSMakeRange(range.location, text.length - range.location)];
    [self setAttributedText:attrString];
}

@end
